$(document).ready(function(){
	initTag();
});

//获取标签列表


function showTag(tagList){
	for (var i=0;i<tagList.length;i++) {
		$("#tagList").append('<tr  id="tag_'+tagList[i].id+'"><td class="tagName">'+tagList[i].name+'</td><td>'+tagList[i].createtime.split(".")[0]+'</td><td><button class="update_btn" onclick="updateTag(this)">修改</button> <button class="delete_btn" onclick="delTag(this)">删除</button> </td><td></td></tr>');
	}
}

function initTag(){
	var url = "/HyperIT/tag/findTag";
	$.get(url,function(data){
		if(data.serviceResult){
			var tagList = data.resultParm.taglist;
			showTag(tagList);
		}
		else{
			alert(data.resultInfo);
		}
	});
	
}






//增加标签

function addTag(){
	
	var tagName = prompt("请输入新的标签：","");
	 if(tagName !=" " && tagName!=null && tagName!="")
 	{
 		var url = "/HyperIT/tag/saveTag";
 		var parm = "name="+tagName;
   	 	$.post(url,parm,function(data){
   	 		if(data.serviceResult){
   	 			var tr = '<tr  id="tag_'+data.resultParm.tag.id+'"><td class="tagName">'+tagName+'</td><td>'+data.resultParm.tag.createtime.split(".")[0]+'</td><td><button class="update_btn" onclick="updateTag(this)">修改</button> <button class="delete_btn" onclick="delTag(this)">删除</button> </td><td></td></tr>'
 				$("#tagList").append(tr);
 				alert(data.resultInfo);
   	 		}
   	 		else{
   	 			alert(data.resultInfo);
   	 		}
   	 	});
 		
 	}
 	 else{
 	 	return falae;
 	 }

}

//删除标签

function delTag(which) {
		var $thisTr;
		$thisTr = $(which).parent().parent();
		var id = $thisTr.attr("id").split("_")[1];
    	var msg = "确定要删除吗？";
			if (confirm(msg)==true){
				var url = "/HyperIT/tag/deleteTag";
				var parm = "id="+id;
				$.post(url,parm,function(data){
					if(data.serviceResult){
						$thisTr.remove();
					}
					else{
						alert(data.resultInfo);
					}
				});
        	}
			else{
				return false;
			}
}

//修改标签

var $thisTag;
function updateTag(which) {
	$(".mask").show();
	$thisTag = $(which).parent().parent();
	$(".btn1").click(function(){
		var newTag = $("#name").val();
		var url = "/HyperIT/tag/updateTag";
		var parm = "id="+$thisTag.attr("id").split("_")[1]+"&name="+newTag;
		$.post(url,parm,function(data){
			if(data.serviceResult){
				$thisTag.children(".tagName").text(newTag);
				maskHidden();
			}
			else{
				alert(data.resultInfo);
			}	
		});
	});
	$("#name").val("");
}

//隐藏弹窗

function maskHidden(){
	$(".mask").hide();
}
